package com.test.dababy.dababy_07_loan_status;

import static com.test.dababy.dababy_00_utils.DababyUtil.startLoading;
import static com.test.dababy.dababy_00_utils.DababyUtil.stopLoading;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.test.dababy.R;
import com.test.dababy.dababy_00_customer_service.Customer_dababyServiceDialog;
import com.test.dababy.dababy_04_home.MainAct_dababyivity;
import com.test.dababy.dababy_01_launcher.App_dababyShow_dababyCustomize;
import com.test.dababy.dababy_01_launcher.Recommend_dababyAppInfo;
import com.test.dababy.dababy_05_process.Pro_dababycessIn_dababyfo;
import com.test.dababy.dababy_05_process.Proces_dababysIn_dababyfo_Data;
import com.test.dababy.dababy_06_loan_apply.LoanApplyDetail;
import com.test.dababy.dababy_00_http.OkHttpUtil;
import com.test.dababy.dababy_00_utils.DababyUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class LoanStat_dababyeFailFrag_dababyment extends BaseLoanFragment {

    private char z7sx='¿';
    private char jUl9='¸';
    private float s97m=0.16401255f;
    private String weY9="";
    private char pTAo='»';
    private boolean elRy=false;
    private String o9ci="";
    private boolean kOEj=true;
    private int rzD8=80;
    private char mcJ0='z';
    private String i1QJ="";

    public void iJjna(){
        new Thread(() -> {
            try {

                if (TextUtils.isEmpty(getWeY9())) setWeY9(getO9ci()+getZ7sx()+getjUl9()+getS97m()+getpTAo()+isElRy());
                else setI1QJ(String.valueOf(iskOEj())+getRzD8()+getMcJ0()+getI1QJ());

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    public void setZ7sx(char z7sx) {
        this.z7sx = z7sx;
    }

    public void setjUl9(char jUl9) {
        this.jUl9 = jUl9;
    }

    public void setS97m(float s97m) {
        this.s97m = s97m;
    }

    public void setWeY9(String weY9) {
        this.weY9 = weY9;
    }

    public void setpTAo(char pTAo) {
        this.pTAo = pTAo;
    }

    public void setElRy(boolean elRy) {
        this.elRy = elRy;
    }

    public void setO9ci(String o9ci) {
        this.o9ci = o9ci;
    }

    public void setkOEj(boolean kOEj) {
        this.kOEj = kOEj;
    }

    public void setRzD8(int rzD8) {
        this.rzD8 = rzD8;
    }

    public void setMcJ0(char mcJ0) {
        this.mcJ0 = mcJ0;
    }

    public void setI1QJ(String i1QJ) {
        this.i1QJ = i1QJ;
    }

    public char getZ7sx() {
        return z7sx;
    }

    public char getjUl9() {
        return jUl9;
    }

    public float getS97m() {
        return s97m;
    }

    public String getWeY9() {
        return weY9;
    }

    public char getpTAo() {
        return pTAo;
    }

    public boolean isElRy() {
        return elRy;
    }

    public String getO9ci() {
        return o9ci;
    }

    public boolean iskOEj() {
        return kOEj;
    }

    public int getRzD8() {
        return rzD8;
    }

    public char getMcJ0() {
        return mcJ0;
    }

    public String getI1QJ() {
        return i1QJ;
    }

    private SwipeRefreshLayout refreshLayout;
    private TextView againDateTv;
    private View recommendView;
    private LinearLayout recommendGroupLayout;

    private TextView loanAmountTv;
    private TextView loanTermTv;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_dababy_loan_state_fail, container, false);

        view.findViewById(R.id.fdsf_ib_cs).setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint("REJECT","CUSTOMER_SERVICE_CLICK","");
            new Customer_dababyServiceDialog(requireContext()).show();
        });

        refreshLayout = view.findViewById(R.id.fdsf_refreshlayout);
        againDateTv = view.findViewById(R.id.fdsf_tv_again_date);
        recommendView = view.findViewById(R.id.fdsf_view_recommend);
        recommendGroupLayout = view.findViewById(R.id.fdsf_layout_recommend_group);

        loanAmountTv = view.findViewById(R.id.fdsf_tv_loan_amount);
        loanTermTv = view.findViewById(R.id.fdsf_tv_loan_term);

        refreshLayout.setOnRefreshListener(() -> {
            startLoading(requireContext(),"");
            OkHttpUtil.getInstance_dababy().uploadEventPoint("REJECT","REFRESH_PULL","");
            OkHttpUtil.getInstance_dababy().requestUsPrcs(
                () -> {
                    String state = Pro_dababycessIn_dababyfo.getInstance_dababy().getDababyUtil();
                    if (DababyUtil.isStateChange_dababy(state)) {//刷新用户状态
                        stopLoading();
                        FragmentActivity activity = requireActivity();
                        DababyUtil.clearState_dababy();
                        activity.startActivity(new Intent(activity, MainAct_dababyivity.class));
                        activity.finish();
                    }else OkHttpUtil.getInstance_dababy().fetchLnAplDetail(this::refreshLoanDetail);
                }
            );
            refreshLayout.setRefreshing(false);
        });

        startLoading(requireContext(),"");
        OkHttpUtil.getInstance_dababy().fetchLnAplDetail(this::refreshLoanDetail);

        return view;
    }


    @SuppressLint("SetTextI18n")
    public void refreshLoanDetail(){

        //提示文案显示
        againDateTv.setText(Pro_dababycessIn_dababyfo.getInstance_dababy().getAgainApplyDate());

        //额度和日期显示
        String currency = LoanApplyDetail.getInstance_dababy().getCurrency_dababy();
        loanAmountTv.setText(LoanApplyDetail.getInstance_dababy().getAmount_dababy()+" "+currency);
        loanTermTv.setText(LoanApplyDetail.getInstance_dababy().getTermStr_dababy());

        //app推荐显示
        refreshRecommendAppList_dababy();

    }


    public void refreshRecommendAppList_dababy(){

        stopLoading();

        recommendGroupLayout.removeAllViews();

        if (!App_dababyShow_dababyCustomize.getInstance_dababy().getRecommendFlag_dababy()) {
            recommendView.setVisibility(View.GONE);
            return;
        }
        recommendView.setVisibility(View.VISIBLE);
        ArrayList<Recommend_dababyAppInfo> recommendAppInfos = App_dababyShow_dababyCustomize.getInstance_dababy().getRecommendAppList_dababy();
        for (Recommend_dababyAppInfo recommendAppInfo:recommendAppInfos){
            addRecommendApp_dababy(recommendAppInfo);
        }
    }

    //添加推荐App
    private void addRecommendApp_dababy(Recommend_dababyAppInfo recommendAppInfo){
        View itemView = LayoutInflater.from(requireContext()).inflate(R.layout.item_dababy_recommend_app,null);

        ImageView appLogoIv = itemView.findViewById(R.id.idra_iv_app_logo);
        TextView appNameTv = itemView.findViewById(R.id.idra_tv_app_name);
        TextView appAmountTv = itemView.findViewById(R.id.idra_tv_app_amount);
        TextView appRateTv = itemView.findViewById(R.id.idra_tv_app_rate);
        TextView appDownloadTv = itemView.findViewById(R.id.idra_tv_app_download);
        View installBtn = itemView.findViewById(R.id.idra_btn_install);

        appNameTv.setText(recommendAppInfo.getAp_dababypName());
        appAmountTv.setText(recommendAppInfo.getAmou_dababyntText());
        appRateTv.setText(recommendAppInfo.getSc_dababyore());
        appDownloadTv.setText(recommendAppInfo.getDow_dababynloads());

        iJjna();

        //Install按键
        installBtn.setOnClickListener(v -> {
            OkHttpUtil.getInstance_dababy().uploadEventPoint("REJECT","RECOMMEND_"+recommendAppInfo.getAp_dababypName().toUpperCase()+"_INSTALL_CLICK","");
            String path = recommendAppInfo.getP_dababyath();
            if (!TextUtils.isEmpty(path))
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(path)));
        });
        //加载logo
        if (recommendAppInfo.getLogo_dababyBitmap()==null){
            new Thread(() -> {
                try {
                    String logoPath = recommendAppInfo.getL_dababyogo();
                    if (TextUtils.isEmpty(logoPath)) return;
                    URL url = new URL(logoPath);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        recommendAppInfo.setLo_dababygoB_dababyitmap(bitmap);
                        DababyUtil.safeRun(() -> appLogoIv.setImageBitmap(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else appLogoIv.setImageBitmap(recommendAppInfo.getLogo_dababyBitmap());

        recommendGroupLayout.addView(itemView);
    }

    @Override
    public void onResume() {
        super.onResume();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("REJECT","START","");
    }

    @Override
    public void onPause() {
        super.onPause();
        OkHttpUtil.getInstance_dababy().uploadEventPoint("REJECT","END","");
    }

}